<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
	<script src="https://cdn.bootcdn.net/ajax/libs/moment.js/2.30.1/moment.min.js"></script>
  </head>
  <body>
    <script>
      const randomSetting = {
        enable: true,
        // enable: false,
        // count: 9999,
        count: 99,
        digit: 4
      }
      const generateSortedRandomGroups = (existingGroupsSet) => {
        const numbers = Array.from({ length: 49 }, (_, i) => i + 1)

        let groups = []
        let attempts = 0

        // 循环确保生成的每组数据独一无二
        while (groups.length < 10) {
          // 打乱顺序
          const shuffled = numbers.sort(() => Math.random() - 0.5)

          // 分组，并对子数组升序排序
          const tempGroups = []
          for (let i = 0; i < shuffled.length; i += 5) {
            const group = shuffled.slice(i, i + 5).sort((a, b) => a - b)
            tempGroups.push(group)
          }

          // 按子数组首个元素排序
          tempGroups.sort((a, b) => a[0] - b[0])

          // 转换为字符串形式，检查是否重复
          const tempGroupsStr = tempGroups.map((group) => JSON.stringify(group))
          const isUnique = tempGroupsStr.every((groupStr) => !existingGroupsSet.has(groupStr))

          if (isUnique) {
            tempGroupsStr.forEach((groupStr) => existingGroupsSet.add(groupStr)) // 添加到已存在集合
            groups = tempGroups // 更新当前分组
          }

          attempts++
          if (attempts > 1000) break // 防止死循环
        }

        return groups
      }
      // 随机分组 001-999
      const generateRangeGroups = () => {
        const groups = {}
        const existingGroupsSet = new Set() // 用于检查全局唯一性

        for (let i = 1; i <= randomSetting.count; i++) {
          const key = `FIVE_RANGE_${String(i).padStart(randomSetting.digit, '0')}_GROUPS`
          groups[key] = generateSortedRandomGroups(existingGroupsSet)
        }

        return groups
      }

      let rangeGroups = {}
      if (randomSetting.enable) {
        rangeGroups = generateRangeGroups()
        console.group(moment().format('YYYY-MM-DD_HH-mm-ss') + ' Five Range Groups')
        console.log(rangeGroups)
        console.groupEnd()
      }
    </script>
  </body>
</html>
